home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_09_06
/
9n06118a
< prev
next >
Wrap
Text File
|
1991-04-21
|
2KB
|
98 lines
#include <stdio.h>
#include <stdlib.h>
FILE *testfile;
float r();
int w();
main()
{
float f;
int rc;
/*open test file */
testfile = fopen("test.dat", "rb+");
if (testfile == NULL)
{
printf("MAIN : open error\n");
return;
}
/* write a floating point number in the file */
f = 123.45;
rewind(testfile);
rc = fwrite(&f, sizeof(float), 1, testfile);
if (rc != 1)
{
printf("MAIN : write error\n");
return;
}
printf("MAIN : \tf = %f\n", f);
/* call subroutine to read the floating point number */
f = r();
/* call subroutine to write the floating point number */
f = 123.45;
w(f);
/* call subroutine to read the floating point number */
f = r();
fclose (testfile);
return;
}
/****************************************************************
* subroutine R : read a floating point number from test file
*****************************************************************/
float
r()
{
float f;
int rc;
rewind(testfile);
rc = fread(&f, sizeof(float), 1, testfile);
if (rc !=1)
{
printf("R : read error\n");
exit(0);
}
printf("R : \tf = %f\n", f);
return f;
}
/****************************************************************
* subroutine W : write a floating point number in test file
****************************************************************/
int
w(f)
float f;
{
int rc; /* return code */
printf("W : \tf = %f\n", f);
rewind(testfile);
rc = fwrite(&f, sizeof(float), 1, testfile);
if (rc != 1)
{
printf("W : write error\n");
exit(0);
}
return 1;
}
/*********
* OUTPUT
**********
MAIN : f = 123.449997
R : f = 123.449997
W : f = 123.450000
R : f = -107374184.000000
*/